package com.shawn.baseappbygroovydsl.ui.views

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.choryan.quan.videowzproject.extension.ExtensionView.hide
import com.choryan.quan.videowzproject.extension.ExtensionView.show
import com.shawn.baseappbygroovydsl.R
import com.shawn.baseappbygroovydsl.component.ItemDecorationBottomOffset
import com.shawn.baseappbygroovydsl.databinding.FragmentNestedBinding
import com.shawn.baseappbygroovydsl.ui.adapter.AdapterHotList
import com.shawn.baseappbygroovydsl.ui.base.FragmentBase
import com.shawn.baseappbygroovydsl.bean.BeanSearchHot

class NestedFragment : FragmentBase<FragmentNestedBinding>(R.layout.fragment_nested) {
    private lateinit var slideshowViewModel: ViewModel

    private val adapterHotList by lazy {
        val value = AdapterHotList()
        value.setOnItemClickListener { adapter, view, position ->
            // item 点击事件
        }
        value.setOnItemLongClickListener { adapter, view, position ->
            // item 长按事件
            true
        }
        value.addOnItemChildClickListener(R.id.iv_thumb) { adapter, view, position ->
            // item 子控件点击事件
        }
        value
    }

    override fun statusBar() {

    }

    override fun onKeyCodeBack(): Boolean {
        return false
    }

    override fun lazyLoad() {
        slideshowViewModel = ViewModelProvider(this)[NestedViewModel::class.java]
        initView()
    }

    override fun initClick() {

    }

    private fun initView() {
        initRv()
    }

    private fun initRv() {
        val manager1 = LinearLayoutManager(requireContext())
        mBind.rv1.adapter = adapterHotList
        mBind.rv1.layoutManager = manager1
        mBind.rv1.addItemDecoration(ItemDecorationBottomOffset())

        val manager2 = LinearLayoutManager(requireContext())
        mBind.rv2.adapter = adapterHotList
        mBind.rv2.layoutManager = manager2
        mBind.rv2.addItemDecoration(ItemDecorationBottomOffset())

        val manager3 = LinearLayoutManager(requireContext())
        mBind.rv3.adapter = adapterHotList
        mBind.rv3.layoutManager = manager3
        mBind.rv3.addItemDecoration(ItemDecorationBottomOffset())
        adapterHotList.submitList(getRvData())
    }

    private fun getRvData(): List<BeanSearchHot> {
        val data = mutableListOf<BeanSearchHot>()
        for (index in 0 until 10) {
            val bean = BeanSearchHot("1", index)
            data.add(bean)
        }
        return data
    }

    override fun getData() {
        refreshHistory()
    }

    override fun getBind(view: View): FragmentNestedBinding {
        return FragmentNestedBinding.bind(view)
    }

    private fun refreshHistory() {
        val history = mutableListOf("A","B","C","D")
        val childViews = mBind.llHistoryLabel.flowCategory.referencedIds.map { id ->
            mBind.llHistoryLabel.root.findViewById<View>(id)
        }
        if (history.isEmpty()) {
            childViews.forEach {
                it.hide()
            }
            mBind.llHistoryLabel.root.hide()
            return
        }
        mBind.llHistoryLabel.root.show()
        history.forEachIndexed { _index, _str ->
            if (_index < childViews.size) {
                val child = childViews[_index]
                if (child is TextView) {
                    child.show()
                    child.text = _str
                    child.tag = _str
                    child.setOnClickListener {

                    }
                }
            }
        }
    }

    companion object {
        const val type1 = "type"
        fun newInstance(): NestedFragment {
            val args = Bundle()
            val fragment = NestedFragment()
            fragment.arguments = args
            return fragment
        }
    }
}